package cn.gary.oaserver.config.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author: 邓必宏
 * @title:
 * @Demo:
 * @DateTime: 2024/4/5
 */
@Configuration
public class RedisConfig {

    // 配置redis中RediTemplate的bean实例对象，并设置相关key-value的序列化器
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String ,Object> redisTemplate = new RedisTemplate<>();
        // 设置RedisTemplate中key序列器为String类型的key序列器
        redisTemplate.setKeySerializer(new StringRedisSerializer ());
        // 设置RedisTemplate中value序列器为String类型的value序列器
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer ());
        // 设置RedisTemplate中Hash类型的key序列器为String类型的key序列器
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // // 设置RedisTemplate中Hash类型value序列器为String类型的value序列器
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
// 设置redis的连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}
